package com.iims.flowable.flow.vo.request;

import cn.hutool.core.lang.Validator;
import com.iims.common.core.page.PageDomain;
import com.iims.common.utils.StringUtils;
import com.iims.flowable.flow.ObjectUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.flowable.common.engine.api.query.Query;
import org.flowable.engine.repository.ProcessDefinitionQuery;

/**
 * 流程定义搜索视图
 *
 * @author fanpei
 * @date 2021/6/10 10:39
 */
@Data
public class FlowProcDefQueryVo extends PageDomain implements IBaseQueryVo<ProcessDefinitionQuery> {
    /**
     * 流程名称
     */
    @ApiModelProperty(value = "流程名称")
    private String name;
    /**
     * 激活挂起状态[1激活 0挂起]
     */
    @ApiModelProperty(value = "激活挂起状态:值来源于字典sys_normal_disable")
    private String status;
    /**
     * 分类
     */
    @ApiModelProperty(value = "流程分类:精确匹配")
    private String category;

    @Override
    public PageDomain selectPageDomain() {
        return this;
    }

    @Override
    public Query genSearchQuery(ProcessDefinitionQuery query) {
        if (Validator.isNotEmpty(name)) {
            query = query.processDefinitionNameLike(ObjectUtils.convertToLike(name));
        }
        if (Validator.isNotEmpty(status)) {
            if ("1".equals(status))
                query = query.active();
            else
                query = query.suspended();
        }
        if (Validator.isNotEmpty(category)) {
            query = query.processDefinitionCategoryLike(ObjectUtils.convertToLike(category));
        }
        if (StringUtils.isEmpty(this.getOrderByColumn())) {
            query = query.orderByProcessDefinitionId().desc();
        }

        return query;
    }

}
